package mx.vlabs.soapdroid.wsdl2java;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;

import org.apache.commons.io.IOUtils;

public class ArtifactLoader {
	
	public static String ARTIFACT_RESOURCE_PATH = "mx/vlabs/soapdroid/wsdl2java/artifacts";
	public static String JAVA_EXTENSION = ".java";
	public static String DIR_SEPARATOR = "/";
	private String artifactId;
	private String artifactLocation;
	
	public ArtifactLoader(){}
	
	public ArtifactLoader(String artifactId,String artifactLocation){
		this.artifactId = artifactId;
		this.artifactLocation = artifactLocation;
	}
	
	public Artifact load(){
		return load(artifactId,artifactLocation);
	}
	
	public Artifact load(String artifactId,String artifactLocation){
		this.artifactId = artifactId;
		this.artifactLocation = artifactLocation;
		String contents = null;
		
		try {
			contents = readResourceContents();
		} catch (IOException e) {}
		
		return new Artifact(artifactId, contents);
	}
	

	
	private String readResourceContents() throws IOException{
		InputStream inputStream = 
				ArtifactLoader.class.getClassLoader()
					.getResourceAsStream(
							ARTIFACT_RESOURCE_PATH+DIR_SEPARATOR+
							artifactLocation+DIR_SEPARATOR+
							artifactId+JAVA_EXTENSION
				);
		
		StringWriter writer = new StringWriter();
		IOUtils.copy(inputStream, writer);

		return writer.toString();
	}
	
	

}
